package com.cjm.algorithm.node;

import com.cjm.algorithm.ListNode;
import com.cjm.algorithm.ListNodeHelp;

public class Ti24042519 {
    public static void main(String[] args) {
        ListNode listNode = ListNodeHelp.toListNode(new int[]{1});
       Ti24042519 t = new Ti24042519();
        ListNode listNode1 = t.removeNthFromEnd(listNode, 1);
        System.err.println("OK");
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode slow = dummy;
        ListNode fast = dummy;
        fast = fast.next;
        while (n > 0 && fast != null) {
            fast = fast.next;
            n--;
        }

        while (fast!= null) {
            fast = fast.next;
            slow = slow.next;
        }

        ListNode target = slow.next;
        slow.next = target.next;
        return dummy.next;
    }
}
